﻿@model IEnumerable<GameSchool.Models.dbLINQ.LectureModel>

<h2>Fyrirlestrar</h2>

<div class="span4">
    @foreach (var video in Model)
    {
        <p><a href="#" class="video_link" id='video_ @video.ID' onclick="get_video(@video.ID)"> @video.Name @video.Points XP</a></p>
    }
</div>

<div class="span7">
    <div id="video_span"></div>
</div>



<script id="comment_template" type="text/html">
    <li class ="comments" id="comment_${ID}">
        <div class="commentBody">
           <a href="http://www.pen.is"><img src='${UserImage}' alt="Notandi" </a>
			<a href="http://www.pen.is">${Name}</a>
			<span>${CommentText}</span>
			<div>
				<abbr>${LongCommentDate} ${ShortCommentDate}</abbr>
				<a class="like" id="${ID}"><i class="icon-thumbs-up"></i></a>
                <p id="like_${ID}"></p>
			</div>
		</div>
	</li>
</script>

<!--Comment Template-->
<script id="addComment_template" type="text/html">
  <div id="AddComment">
      <ul class="commentList">
			    <textarea id="CommentText" name="CommentText" rows="3" cols="20"></textarea>
			    <label>
				    <input id="${ID}" class="submit_comment" type="submit" value="Skrifa athugasemd" />
			    </label>
			    @Html.ValidationMessage( "CommentText" )
        </ul>   
    </div>
</script>

<!--Likes Template For One-->
<script id="like_template" type="text/html">
    <a class="liker" href="http://www.ru.is">${UserName}</a> líkar þetta <br>
</script>

<!--Likes Template For More than One-->
<script id="like_template_two" type="text/html">
    <abbr title="test">test</abbr>
</script>

<!--Error Template-->
    <script id="error_template" type="text/html">
        <p id="error">Slakaðu á djeng, þú ert nú þegar búinn að líka þetta</p>
    </script>

<!--Video Template-->
<script id="video_template" type="text/html">
    <h3>${Name}</h3>
    <p>${Description}</p>
    <iframe width="420" height="315" src="${Source}" frameborder="0" allowfullscreen="allowfullscreen" id="${ID}"></iframe>
</script>

<script type="text/javascript">
    $(document).ready(function () {
        //MÖGULEGA FÆRA EITTHVAÐ HINGAÐ INN TIL AÐ LAGA RANDOM SKIPTIN SEM AÐ KOMMENTIN DETTA EKKI INN
    });

    //Gets video, comment and likes
    function get_video(id) {
        $("#video_span > *").remove();
        $.getJSON("/Comment/GetLectureByID", { id: id }, function (data) {
            $("#video_template").tmpl(data).appendTo("#video_span");
            $("#addComment_template").tmpl(data).appendTo("#video_span");
        });
        $.getJSON("/Comment/GetCommentsByID", { id: id }, function (data) {
            $("#comment_template").tmpl(data, get_likes(id)).insertBefore("#AddComment");
        });
        /*$.getJSON("/Student/GetCommentsByID", { id: id }, function (data) {
            $("#comment_template").tmpl(data, get_likes(id)).insertBefore("#AddComment");
        });*/
        get_likes(id);/* {
            $.getJSON("/Student/GetLikesForLecture", { LectureID: id }, function (data) {
                for (var i = 0; i < data.length; i++) {
                    var item = data[i];
                    $("#like_template").tmpl(item).appendTo("#like_" + item.CommentID);
                }
            });
        }*/
    }
    function get_likes(like_id) {
        $.getJSON("/Comment/GetLikesForLecture", { LectureID: like_id }, function (data) {
            for (var i = 0; i < data.length; i++) {
                var item = data[i];
                $("#like_template").tmpl(item).appendTo("#like_" + item.CommentID);
            }
        });
    }
    /*//Get more comments!
    $(".get_more_comments").live("click", function () {
        var lecid = parseInt($(this).attr('id'));
        $.getJSON("/Student/GetMoreCommentsByID", { id: lecid }, function (data) {
            $("#comment_template").tmpl(data, get_likes(lecid)).insertBefore("#AddComment");
        });
        function get_likes(lecid) {
            $.getJSON("/Student/GetLikesForLecture", { LectureID: lecid }, function (data) {
                for (var i = 0; i < data.length; i++) {
                    var item = data[i];
                    $("#like_template").tmpl(item).appendTo("#like_" + item.CommentID);
                }
            });
        }
        $(".get_more_comments").hide();
    });*/
    
    //Post a comment!
    $(".submit_comment").live("click", function () {
        var lecid = parseInt($(this).attr('id'));
        var param = $("#CommentText").val();
        $.post("/Comment/CreateCommentForLecture", { CommentText: param, id: lecid }, function (data) {
            // processComments(data.Comments);
            $(".comments").remove();
            $("#comment_template").tmpl(data).insertBefore("#AddComment");
            get_likes(lecid);
            $("#CommentText").val("");
        });
    });

    function processComments(data) {
        for (var i = 0; i < data.length; i++) {
            data[i].CommentDate = ConvertStringToJSDate(data[i].CommentDate);
        }
    }
    function ConvertStringToJSDate(date) {
        var dtE = /^\/Date\((-?[0-9]+)\)\/$/.exec(date);
        if (dtE) {
            var dt = new Date(parseInt(dtE[1], 10));
            return dt;
        }
        return null;
    }

    function toShortDate(dt) {
        var currentTime = new Date();

        var diff = (currentTime - dt) / 1000;

        if (diff < 10) {
            return "fyrir nokkrum sekúndum";
        }
        else if (diff >= 10 && diff < 60) {
            return "fyrir minna en mínútu síðan";
        }
        else if (diff >= 60 && diff < 120) {
            return "fyrir einni mínútu síðan";
        }
        else if (diff >= 120 && diff < 3600) {
            var minutes = parseInt(diff / 60);
            return "fyrir " + minutes + " mínútum síðan";
        }
        else if (diff >= 3600 && diff < 86400) {
            var hours = parseInt(diff / 360);
            return "fyrir" + hours + " tímum síðan";
        }
        else {
            return "more than one day ago";
        }
    }

    //Post a like!
    $(".like").live("click", (function () {
        var like = parseInt($(this).attr('id'));
        $.post("/Comment/CreateLikeForComment", { CommentID: like }, function (data) {
            if (data != "") {
                $("#like_template").tmpl(data).appendTo("#like_" + like);
            }
            else {
                $("#error_template").tmpl(data).appendTo("#like_" + like);
                $("#error").fadeOut(5000, function () {
                    $("#error").remove();
                });
            }
        });
    }));    

</script>

<!--@foreach(var item in Model)
{
    <h3>@item.Name</h3>
    <p>@item.Description</p>
    <iframe width="420" height="315" src="@item.Source" frameborder="0" allowfullscreen="allowfullscreen"></iframe>
}-->

